package com.blood.core.util.token;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.blood.core.config.AudienceConfig;
import com.blood.core.resp.TokenResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yelong
 * @version 1.0
 * @date 2020/8/12 16:28
 * @description
 */
@Service
@Slf4j
public class TokenService {
    @Autowired
    private AudienceConfig audienceConfig;

    /**
     * 获取token
     *
     * @param id          用户id
     * @param serviceType 客户类型
     * @return
     */
    public TokenResp getToken(String id, String serviceType) {
        TokenResp resp = new TokenResp();
        DateTime now = DateTime.now();
        DateTime expiredTime = DateUtil.offsetDay(now, audienceConfig.getExpiresDay());
        String token = JwtTokenUtil.createJWT(
                id,
                now,
                expiredTime,
                serviceType,
                audienceConfig.getBase64Secret());
        resp.setAccessToken(JwtTokenUtil.TOKEN_PREFIX + token);
//        String data = id + ","  + serviceType;
//        try {
//            String encryptByPublicKey = RSA.encryptByPublicKey(data, sysData.getPublicKey());
//            resp.setRefreshKey(sysData.getPublicKey());
//            resp.setRefreshSign(encryptByPublicKey);
//        } catch (Exception e) {
//            log.error("获取刷新token签名时Rsa加密异常,serviceType:{}", serviceType, e);
//            throw new BusinessException(GlobalResultStatus.FAIL);
//        }
        resp.setUserId(id);
        return resp;
    }


}
